﻿<cfsilent>
	<cfscript>

		/* 处理当前排课学期数据 */
		event.setArg("pageTitle", "教学运行管理 - CET照片下载");
		
		examId = event.getArg("EX");
		termId = event.getArg("TID");
		
		zipUUID = termId & examId;
		
		zipFile = getProperty("cdnAssetsPath") & "/tmp/stuPictureCET/" & zipUUID & ".zip";
		
		currentTab = event.getArg("TabID", "labPackage");
		
		passArgs = structNew();
		structInsert( passArgs, "EX", examId, true );
		structInsert( passArgs, "TID", termId, true );
		structInsert( passArgs, "UUID", termId & examId, true );

		/* 生成已报名 且有电子照片的考生名单 */
		sql = "SELECT 
				t_student_status.stu_id 
				FROM t_cet_entry 
				INNER JOIN t_student_status ON t_cet_entry.stu_id = t_student_status.stu_id 
				WHERE 
					term_id = :termId 
					AND 
					exam_id = :examId 
					AND 
					is_charged = '1' 
					AND 
					substr( t_student_status.student_prop, 18, 1 ) = '1' 
				ORDER BY t_student_status.stu_id ";
		
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="examId", value=examId, cfsqltype="cf_sql_char" );

		rs_student = queryObj.execute( sql=sql ).getResult();
		
		studentArray = [];
		
		for ( a=1; a LTE rs_student.recordCount; a++ ) {
			arrayAppend( studentArray, rs_student["stu_id"][a] );
		}
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('cetState')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">报名状态</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 db"></i>批量导出<cfoutput>#examId#</cfoutput>的学生照片
				</h2>
			</div>
		</div>
	</div>
</div>
<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="export">
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labPackage">class="active"</cfif> id="labPackage" tabTarget="Package">照片下载</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					
					// -->
					//]]>
				</script>
				
				<div id="Package" class="tabContent">
					
					<div class="noticeBlock">
						<div class="operation">
							<cfif arrayLen(studentArray)>
								<a class="uiButton" href="javascript:processPictures(true);"><i class="mrs img btnPublish"></i><span class="uiButtonText">重新打包照片</span></a>
							</cfif>
						</div>
						<h3><cfoutput>#examId#</cfoutput> 照片导出</h3>
						<p><span class="img icon16x16 message"></span>此项功能可帮助您将学生电子照片打包并导入到外语等级考试管理系统中.</p>
						<hr/>
					</div>
					
					<cfif arrayLen(studentArray)>
							
							<div id="picPacking">
								
								<div class="mtl mbs">
									<img src="<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/graphic/waiting.gif" />
								</div>
							
								<div class="UIProgressbar">
									<span class="progressBar" id="pb1">0%</span>
									<p id="output">处理队列初始化</p>
									<p id="info"></p>
								</div>
								
							</div>
							
							<div id="picComplete" class="clearfix hidden_elem">
								<ul class="UIDashBoard">
									<li class="col2">
										<div class="board hasPic">
											<a href="<cfoutput>#buildURL("studentPhotoDownloadZip", passArgs)#</cfoutput>">
												<div class="pic">
													<span class="icon64x64 Save"></span>
												</div>
												<div class="content">
													<h3><cfoutput>#examId#</cfoutput><span class="unitPriod">JPEG</span></h3>
													<h5>考生照片</h5>
													<p class="describ">压缩包内是成功报名的考生照片, 下载后请解压再导入报名系统中</p>
												</div>
											</a>
										</div>
									</li>
								</ul>
								
							</div>
							
							<div id="out"></div>
							
							<cfset cellPerRow = 500 />
							
							<script language="javascript" type="text/javascript">
								//<![CDATA[
								<!--
								<!--- 每 $cellPerRow$ 张照片一组 --->
								var picData = [
												<cfif arrayLen(studentArray) gt cellPerRow >
													<cfloop from="1" to="#Ceiling( arrayLen(studentArray) / cellPerRow )#" index="g">
														<cfif g lt Ceiling( arrayLen(studentArray) / cellPerRow )>
															[ <cfloop from="#cellPerRow*(g-1)+1#" to="#cellPerRow*g#" index="c">'<cfoutput>#studentArray[c]#</cfoutput>'<cfif c lt cellPerRow*g>, </cfif></cfloop> ], 
															<cfelse>
															[ <cfloop from="#cellPerRow*(g-1)+1#" to="#arrayLen(studentArray)#" index="c">'<cfoutput>#studentArray[c]#</cfoutput>'<cfif c lt arrayLen(studentArray)>, </cfif></cfloop> ]
														</cfif>
													</cfloop>
													<cfelse>
													[ <cfloop from="1" to="#arrayLen(studentArray)#" index="c">'<cfoutput>#studentArray[c]#</cfoutput>'<cfif c lt arrayLen(studentArray)>, </cfif></cfloop>  ]
												</cfif>
												];
								
								
								function processPictures( deleteFile ) {
									
									$("#picPacking").removeClass("hidden_elem");
									$("#picComplete").addClass("hidden_elem");
									
									/* 显示阻挡信息 */
									$.blockUI({ 
												message: '正在打包照片文件...请耐心等待',
												css: { 
														'width': '16%',
														'left': '42%',
														'border': 'none', 
														'padding': '15px 5px', 
														'backgroundColor': '#333', 
														'border-radius': '4px', 
														'opacity': .6, 
														'color': '#fff',
														'cursor': null
														},
												overlayCSS: {
														'backgroundColor': '#aaa', 
														'opacity': .1 ,
														'cursor': null
														}
												});	

									/* init Progress */
									$("#pb1").progressBar({
															boxImage		: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
															barImage		: {
																				0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																				30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																				70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																				}
															});
									
									if ( deleteFile ) {

										/* 异步删除打包文件 */
										$.post(
												"<cfoutput>#buildURL('studentPhotoDeleteZip')#</cfoutput>", 
												{
													'UUID': '<cfoutput>#zipUUID#</cfoutput>',
													'seed': (new Date()).getTime()
												}, 
												function(data) {

													//$("#out").html(data);
													
													response = $.parseJSON(data);
												
													$("#output").html( "PROC-DELETE. " + response.operation);
													
													// 开始分布打包文件
													progressZip(0);
													
												});
										
									}
									else {
										
										/* 直接开始打包 */
										progressZip(0);
										
									}
									
									
								
									return void(0);
								}
								
								function progressZip(current) {

									/* 打包照片文件 */
									$.post(
											"<cfoutput>#buildURL('apiStudentPhotoPacking')#</cfoutput>", 
											{
												'StudentIDs': picData[current].join(","),
												'UUID': '<cfoutput>#zipUUID#</cfoutput>',
												'seed': (new Date()).getTime()
											}, 
											function(data) {

												//$("#out").html(data);
												response = $.parseJSON(data);
												
												$("#output").html( "PROC-" + current + "/" + picData.length + ". " + response.operation);
												
												/* fresh progress bar */
												var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(picData.length));
												
												$("#pb1").progressBar(
																		percentage,
																		{
																			boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																			barImage : {
																				0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																				30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																				70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																			},
																			callback: function(running){
																	
																				if (running.running_value == running.value) {
																	
																					current++;
											
																					if ( current < picData.length) {
																						progressZip(current);
																					}
																					else {
																				
																						$("#output").text("完成");
																						$("#picComplete").removeClass("hidden_elem");
																						$("#picPacking").addClass("hidden_elem");
																						
																						/* 取消阻挡 */
																						$.unblockUI();
																					}
																				}
																			}
																	
																		});
													
											});
									
								}
								
								<cfif fileExists(zipFile)>
								
								$("#picComplete").removeClass("hidden_elem");
								$("#picPacking").addClass("hidden_elem");
								
								<cfelse>
								
								$(document).ready(function() {
									processPictures( false );
								});
					
								</cfif>


								// -->
								//]]>
							</script>
							

						<cfelse>
							<div class="systemNotice">
								<p>当前没有报考等级考试的学生数据</p>
							</div>
					</cfif>
					
					
				</div>
				
			</div>
		</li>
	</ul>
</div>